#include <stdio.h>

struct ListNode {
  int val;
  ListNode *next;
  ListNode (int x) : val(x), next(nullptr) {}
};

ListNode* make (int num) {
  ListNode* first = new ListNode(0);
  ListNode* current = first;

  for (;;) {
    int val = num % 10;
    current->val = val;
    num = num / 10;

    if (num > 0) {
      current = current->next = new ListNode(0);
    } else {
      break;
    }
  }
  return first;
}

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
  auto r = new ListNode(0);
  auto first = r;
  ListNode* a1 = l1;
  ListNode* a2 = l2;
  int ex = 0;

  for (; ;) {
    int sum = (a1 ? a1->val : 0) + (a2 ? a2->val : 0) + ex;
    int thisex = sum / 10;
    sum = sum % 10;

    r->val = sum;

    ex = thisex;

    if (a1) { a1 = a1->next; }
    if (a2) { a2 = a2->next; }

    if (a1 != nullptr || a2 != nullptr) {
      r = r->next = new ListNode(0);
    } else {
      if (ex > 0) {
        r->next = new ListNode(ex);
      }
      break;
    }
  }
  return first;
}

int toNumber (ListNode* li) {
  int number = 0;

  for (int n = 1; li != nullptr; li = li->next) {
    number += li->val * n;
    n = n * 10;
  }

  return number;
}

int main () {
  auto a = make(1);
  auto b = make(99);

  auto r = addTwoNumbers(a, b);
  int num = toNumber(r);

  printf("Hello World %d\n", num);
}